package com.aps.apspda.dialog;

import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatDialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.aps.apspda.R;
import com.aps.apspda.callback.DialogEntityCallBack;
import com.aps.apspda.callback.EntityCallBack;
import com.aps.apspda.callback.StringCommonCallBack;
import com.aps.apspda.entity.BaseEntity;
import com.aps.apspda.entity.LoginEntity;
import com.aps.apspda.utils.AntiShake;
import com.aps.apspda.utils.AppUtils;
import com.aps.apspda.utils.NetUtils;
import com.aps.apspda.utils.StaticMembers;
import com.aps.apspda.utils.ToastUtils;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;

import java.util.HashMap;
import java.util.Map;


/**
 * @author huangxin
 * @date 2015/11/10
 * @desc 通用dialog
 */
public class UserVerify2FragmentDialog extends AppCompatDialogFragment {
    private String tips, leftStr, rightStr, text, text2;
    private View.OnClickListener onClickListener;
    private StringCommonCallBack callBack;
    private String TAG_USER_LOGIN = "user_login";
    @Override
    public void show(FragmentManager manager, String tag) {
        if (!isAdded()) {
            FragmentTransaction ft = manager.beginTransaction();
            ft.add(this, tag);
            ft.commitAllowingStateLoss();
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            dialog.getWindow().setLayout((int) (StaticMembers.SCREEN_WIDTH * 0.8), ViewGroup.LayoutParams.WRAP_CONTENT);
        }
    }

    public void setCallBackAndWhere(String tips, StringCommonCallBack callBack, String text,String text2) {
        this.callBack = callBack;
        this.tips = tips;
        this.text = text;
        this.text2 = text2;
    }

    public void setData(String tips, View.OnClickListener confirmListener) {
        this.tips = tips;
        this.onClickListener = confirmListener;
    }

    public void setData(String tips, String leftStr, String rightStr, View.OnClickListener confirmListener) {
        this.tips = tips;
        this.onClickListener = confirmListener;
        this.leftStr = leftStr;
        this.rightStr = rightStr;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_userverify2, null);
        getDialog().getWindow().setGravity(Gravity.CENTER); // 非常重要：设置对话框弹出的位置
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getDialog().getWindow().setWindowAnimations(R.style.BottomDialog); // 添加动画
        getDialog().setCancelable(false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        Button btnConfirm = view.findViewById(R.id.btnConfirm);//确认按钮
        Button btnCancel = view.findViewById(R.id.btnCancel);//取消按钮
        final EditText etMobile = view.findViewById(R.id.etMobile);//dialog内容 生产用户名
        final EditText etPassword = view.findViewById(R.id.etPassword);//dialog内容 生产密码
        final EditText etENGMobile = view.findViewById(R.id.etMobile2);//dialog内容 工程用户名
        final EditText etENGPassword = view.findViewById(R.id.etPassword2);//dialog内容 工程密码
        // TextView tvTitle = view.findViewById(R.id.tvTitle);
        // tvTitle.setText(tips);
        if (leftStr != null && leftStr.length() > 0) {
            btnCancel.setText(leftStr);
        }
        if (rightStr != null && rightStr.length() > 0) {
            btnConfirm.setText(rightStr);
        }
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        btnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (AntiShake.check(v.getId())) {    //判断是否多次点击
                    ToastUtils.showFreeToast("请勿重复点击",
                            getActivity(), false, Toast.LENGTH_SHORT);
                    return;
                }
                if (etENGMobile.getText().toString().trim().length() == 0) {
                    ToastUtils.showFreeToast("请输入工程用户名",  getActivity(), false, Toast.LENGTH_SHORT);
                    return;
                }
                if (etENGPassword.getText().toString().trim().length() == 0) {
                    ToastUtils.showFreeToast("请输入工程密码",  getActivity(), false, Toast.LENGTH_SHORT);
                    return;
                }
                if (etMobile.getText().toString().trim().length() == 0) {
                    ToastUtils.showFreeToast("请输入生产用户名",  getActivity(), false, Toast.LENGTH_SHORT);
                    return;
                }
                if (etPassword.getText().toString().trim().length() == 0) {
                    ToastUtils.showFreeToast("请输入生产密码",  getActivity(), false, Toast.LENGTH_SHORT);
                    return;
                }

                EntityCallBack<BaseEntity<LoginEntity>> callBack2 = new DialogEntityCallBack<BaseEntity<LoginEntity>>
                        (new TypeToken<BaseEntity<LoginEntity>>() {
                        }.getType(),  getActivity().getSupportFragmentManager(), this) {
                    @Override
                    public void onSuccess
                            (final Response<BaseEntity<LoginEntity>> response) {
                        if (response.body().isSuccess(getActivity())) {
                            LoginEntity loginEntity = response.body().getData();
                            if (loginEntity.getLOGINRESULT()) {
                                ToastUtils.showFreeToast("校验成功",
                                            getActivity(), true, Toast.LENGTH_SHORT);
                                    callBack.onCallback("校验成功,"+etMobile.getText().toString().trim()+","+etENGMobile.getText().toString().trim());

                            } else {
                                ToastUtils.showFreeToast(loginEntity.getMESSAGE(),
                                        getActivity(), true, Toast.LENGTH_SHORT);
                                    callBack.onCallback(loginEntity.getMESSAGE());
                            }
                        } else {
                            final WarnFragmentDialog dialog = new WarnFragmentDialog();
                            dialog.setData(response.body().getMessage());
                            dialog.setListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    dialog.dismiss();
                                }
                            });
                            dialog.show(getActivity().getSupportFragmentManager(), "check_fail_tips");
                            callBack.onCallback(response.body().getMessage());
                        }
                    }

                    @Override
                    public void onError
                            (Response<BaseEntity<LoginEntity>> response) {
                        super.onError(response);
                        AppUtils.saveErrorMessages(response.getException(),  "ClientLogin");
                        ToastUtils.showFreeToast("连接服务器失败",
                                getActivity(), false, Toast.LENGTH_SHORT);
                        callBack.onCallback("连接服务器失败");
                    }
                };

                Map<String, String> map = new HashMap<>();
                map.put("USERNAME", etMobile.getText().toString().trim());//生产用户名
                map.put("PASSWORD", etPassword.getText().toString().trim());//生产密码
                map.put("AREA", "");
                map.put("LOGINTYPE", "CHECK");
                map.put("SUBSTATUS", text);//要切换的设备状态
                map.put("LOGGEDUSER", StaticMembers.CUR_USER.getUSERNAME());
                map.put("FROMSUBSTATUS", text2);//要切出的设备状态
                map.put("ENGINEERNAME", etENGMobile.getText().toString().trim());//工程用户名
                map.put("ENGINEERPASSWORD", etENGPassword.getText().toString().trim());//工程密码
                NetUtils.requestNet(this, "/ClientLogin", map, callBack2);
            }
        });
        super.onViewCreated(view, savedInstanceState);
    }
}
